/*
 * Project Name: jmax
 * File Name: ForumInit.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.forum.init;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xdtech.common.service.impl.BaseService;
import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.common.utils.encrypt.DESCoder;
import com.xdtech.core.init.SysInitOperation;
import com.xdtech.core.model.BaseModel;
import com.xdtech.forum.model.Article;
import com.xdtech.forum.model.Category;
import com.xdtech.forum.model.CommentInfo;
import com.xdtech.forum.model.LinkTag;
import com.xdtech.forum.model.Member;
import com.xdtech.forum.service.ArticleService;
import com.xdtech.forum.service.CategoryService;
import com.xdtech.forum.service.CommentInfoService;
import com.xdtech.forum.service.LinkTagService;
import com.xdtech.forum.service.MemberService;
import com.xdtech.forum.vo.ArticleItem;
import com.xdtech.forum.vo.CategoryItem;
import com.xdtech.forum.vo.CommentInfoItem;
import com.xdtech.forum.vo.LinkTagItem;
import com.xdtech.sys.model.CodeValue;
import com.xdtech.sys.model.DictionaryCode;
import com.xdtech.sys.model.MenuFunction;
import com.xdtech.sys.model.Params;

/**
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-5-19 下午4:06:22
 */
public class ForumInit implements SysInitOperation
{
	Log log = LogFactory.getLog(ForumInit.class);

	/* (非 Javadoc)
	 * initingToDb
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToDb(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToDb(BaseService<BaseModel> baseService)
	{
		List params = (List) baseService.findByHql("from Params where code=? and codeValue=?", "ForumInit","true");
		log.info("论坛模块数据库初始化......");
		if (EmptyUtil.isEmpty(params)) {
			MenuFunction m4 = new MenuFunction();
			m4.setIconName("icon-project");
			m4.setNameCN("案例演示");
			m4.setOperationCode("codecreater-manage");
			baseService.save(m4);
			
			MenuFunction m41 = new MenuFunction();
			m41.setIconName("icon-score");
			m41.setNameCN("商城管理");
			m41.setOperationCode("shop-manage");
			m41.setParent(m4);
			baseService.save(m41);
			
			MenuFunction m42 = new MenuFunction();
			m42.setIconName("icon-forum");
			m42.setNameCN("论坛管理");
			m42.setOperationCode("forum-manage");
			m42.setParent(m4);
			baseService.save(m42);
			
			MenuFunction m421 = new MenuFunction();
			m421.setIconName("icon-category");
			m421.setNameCN("帖子类别");
			m421.setOperationCode("category-manage");
			m421.setPageUrl("category.do?category");
			m421.setParent(m42);
			baseService.save(m421);
			
			MenuFunction m422 = new MenuFunction();
			m422.setIconName("icon-article");
			m422.setNameCN("帖子管理");
			m422.setOperationCode("article-manage");
			m422.setPageUrl("article.do?article");
			m422.setParent(m42);
			baseService.save(m422);
			MenuFunction m423 = new MenuFunction();
			m423.setIconName("icon-member");
			m423.setNameCN("会员管理");
			m423.setOperationCode("member-manage");
			m423.setPageUrl("member.do?member");
			m423.setParent(m42);
			baseService.save(m423);
			
			MenuFunction m424 = new MenuFunction();
			m424.setIconName("icon-save");
			m424.setNameCN("快捷标签");
			m424.setOperationCode("quick-tag");
			m424.setPageUrl("linkTag.do?linkTag");
			m424.setParent(m42);
			baseService.save(m424);
			
			MenuFunction m425 = new MenuFunction();
			m425.setIconName("icon-comment-info");
			m425.setNameCN("评论管理");
			m425.setOperationCode("comment-manage");
			m425.setPageUrl("commentInfo.do?commentInfo");
			m425.setParent(m42);
			baseService.save(m425);
			
			MenuFunction m426 = new MenuFunction();
			m426.setIconName("icon-portal");
			m426.setNameCN("论坛门户");
			m426.setOperationCode("forum-look");
			m426.setPageUrl("portal.action?index");
			m426.setIframe(true);
			m426.setParent(m42);
			baseService.save(m426);
			
			MenuFunction m43 = new MenuFunction();
			m43.setIconName("icon-save");
			m43.setNameCN("运输管理");
			m43.setOperationCode("tran-manage");
			m43.setParent(m4);
			baseService.save(m43);
			
			MenuFunction m431 = new MenuFunction();
			m431.setIconName("icon-save");
			m431.setNameCN("订单管理");
			m431.setOperationCode("tran-order-manage");
			m431.setPageUrl("tranOrder.do?tranOrder");
			m431.setParent(m43);
			baseService.save(m431);
			
			MenuFunction m432 = new MenuFunction();
			m432.setIconName("icon-save");
			m432.setNameCN("司机管理");
			m432.setOperationCode("tran-driver-manage");
			m432.setPageUrl("tranDriver.do?tranDriver");
			m432.setParent(m43);
			baseService.save(m432);
			
			
			Member member = new Member();
			member.setName("max");
			member.setNickName("max");
			member.setEmail("123456@qq.com");
			member.setPassword(DESCoder.encrypt("123456"));
			member.setSex("M");
			baseService.save(member);
			List entities = new ArrayList<Category>();
			entities.add(new Category("1","问题反馈","fa-file-text"));
			entities.add(new Category("2","平台交流","fa-bell-o"));
			baseService.saveAll(entities);
			
			
			List linkTags = new ArrayList<LinkTag>();
			//关键字创建
			linkTags.add(new LinkTag("java", "1", "", ""));
			linkTags.add(new LinkTag("jmax", "1", "", ""));
			linkTags.add(new LinkTag("前端", "1", "", ""));
			linkTags.add(new LinkTag("极速开发平台", "1", "", ""));
			//友情链接地址
			linkTags.add(new LinkTag("百度", "2", "http://www.baidu.com", ""));
			linkTags.add(new LinkTag("个人博客", "2", "http://java4ever.iteye.com", ""));
			//联系
			linkTags.add(new LinkTag("QQ", "3", "", "webpages/forum/portal/images/max.jpg"));
			//广告
			linkTags.add(new LinkTag("广告", "4", "http://www.baidu.com", "webpages/forum/portal/images/max.jpg"));
			baseService.saveAll(linkTags);
			
			Article article = new Article("JMax快速开发平台","JMax由java开发，基于spring+hibernate+easyui整合的一套综合从前端到后端一体化框架,UI自定义封装，方便用户快捷使用，sql模版自定义，灵活配置，代码自动速成，一键开发，图表配置，立即预览",new Category(2L));
			article.setIsHot("true");
			article.setIcon("webpages/forum/portal/images/jmax-home.png");
			article.setMember(new Member(1L));
			baseService.save(article);
			
			article = new Article("报表图表","根据配置的sql和xml，自动完成报表导出和图表显示功能。",new Category(1L));
			article.setIsHot("true");
			article.setMember(new Member(1L));
			baseService.save(article);
			
			CommentInfo commentInfo = new CommentInfo();
			commentInfo.setContent("好东西，赞一个，支持开源");
			commentInfo.setMember(member);
			commentInfo.setArticle(article);
			baseService.save(commentInfo);
			
			DictionaryCode dictionaryCode = new DictionaryCode("FORUM_LINK_TAG_TYPE","快捷链接类型");
			baseService.save(dictionaryCode);
			baseService.save(new CodeValue("1","关键字",1,dictionaryCode));
			baseService.save(new CodeValue("2","友情链接",2,dictionaryCode));
			baseService.save(new CodeValue("3","联系",3,dictionaryCode));
			baseService.save(new CodeValue("4","广告",4,dictionaryCode));
			Params p = new Params("ForumInit", "true", "论坛模块数据库初始化标记");
			baseService.save(p);
			log.info("论坛模块数据库初始化......结束");
		}else {
			log.info("论坛模块数据库已经初始化......完毕");
		}
		
	}

	/* (非 Javadoc)
	 * initingToCache
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToCache(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToCache(BaseService<BaseModel> baseService)
	{
		// TODO Auto-generated method stub
		
	}

	/* (非 Javadoc)
	 * getInitOrder
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @return
	 * @see com.xdtech.core.init.SysInitOperation#getInitOrder()
	 */
	@Override
	public int getInitOrder()
	{
		return 21;
	}

	/* (非 Javadoc)
	 * initingToWebApplication
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param baseService
	 * @see com.xdtech.core.init.SysInitOperation#initingToWebApplication(com.xdtech.common.service.impl.BaseService)
	 */
	@Override
	public void initingToWebApplication(BaseService<BaseModel> baseService)
	{
		ServletContext application = ApplicationContextUtil.getApplication();
		CategoryService categoryService = (CategoryService) ApplicationContextUtil.getContext().getBean(CategoryService.class);
		List<CategoryItem> categoryItems = categoryService.loadCategoryItems();
		application.setAttribute("categories", categoryItems);
		LinkTagService linkTagService = (LinkTagService) ApplicationContextUtil.getContext().getBean(LinkTagService.class);
		Map<String, List<LinkTagItem>> linkTagMap = linkTagService.loadLinkTagsMap();
		application.setAttribute("linkTagsMap", linkTagMap);
		ArticleService articleService = (ArticleService) ApplicationContextUtil.getContext().getBean(ArticleService.class);
		//加载热门文章
		List<ArticleItem> hotArticleItems = articleService.loadArticleHotItems();
		application.setAttribute("hotArticleItems", hotArticleItems);
		//加载最新5条评论
		CommentInfoService commentInfoService = (CommentInfoService) ApplicationContextUtil.getContext().getBean(CommentInfoService.class);
		List<CommentInfoItem> commentInfoItems = commentInfoService.loadTop5Items();
		application.setAttribute("commentInfoItems", commentInfoItems);
	}

}
